//初始化客户归属下拉列表
function ascription() {
    var user_data = {
    }
    var succCallBack = function (data, status, response) {
        //console.log(data)
        if(data.errorCode=="0") {
            var dataInter=data.group;
            var interText = doT.template($("#ascription_bob").text());
            $("#ascription_lsit").html(interText(dataInter));
        }
    };
    return web.doAjax(root.inter.getGroup, 'post', user_data, succCallBack);
}


var dataSource = new Array();
//table的数据源
var table;   //table对象
dataSource = [];
//查询
function searchsdfs() {
   var datas={}
    if($("#ascription_lsit").val()==""){
        datas={
            "pageNum":1,
            "pageSize":30,
            "name":$("#name").val(),
            "phone":$("#phone").val(),
        }
    }else {
        datas={
            "pageNum":1,
            "pageSize":30,
            "orgId":$("#ascription_lsit").val(),
            "name":$("#name").val(),
            "phone":$("#phone").val(),
        }
    }
    $.ajax({
        url:root.member.searchOrgList,
        cache: false,  //禁用缓存
        type: "POST",
        dataType:'json',
        contentType: "application/json",
        data: JSON.stringify(datas),
        success: function (data) {
            // couponlist(data.result);
            couponlist(data.data, 0);
            $('.M-box3').pagination({
                pageCount: data.data.pageCount,
                jump: true,
                coping: true,
                prevContent: '上页',
                nextContent: '下页',
                callback: function (api) {
                    var datas2={}
                    if($("#ascription_lsit").val()==""){
                        datas2={
                            "pageNum":api.getCurrent(),
                            "pageSize":30,
                            "name":$("#name").val(),
                            "phone":$("#phone").val(),
                        }
                    }else {
                        datas2={
                            "pageNum":api.getCurrent(),
                            "pageSize":30,
                            "orgId":$("#ascription_lsit").val(),
                            "name":$("#name").val(),
                            "phone":$("#phone").val(),
                        }
                    }
                    $.ajax({
                        url: root.member.searchOrgList,
                        cache: false,  //禁用缓存
                        type: "POST",
                        dataType:'json',
                        contentType: "application/json",
                        data: JSON.stringify(datas2),
                        success: function (result) {
                            couponlist(result.data);
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            alert(XMLHttpRequest);
                        }
                    });
                }
            });

        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    });
}
//列表
function couponlist(data, index){

    // var j = 0;
    sessionStorage.setItem("authorizedUsers", JSON.stringify(data));
    dataSource.splice(0,dataSource.length);//清空数组


    if(data !=null && data != ''){
        var datashu =data.startRow+1;


        $.each(data.data, function (i, field) {
            var datashus = datashu++
            var rowData = new Array();
            rowData.push(datashus);
            rowData.push(field.organizationName);
            rowData.push(field.businessUserName);
            rowData.push(field.businessUserPhone);
            rowData.push(field.id);
            dataSource.push(rowData);

        });
    }
    initDataTable(dataSource);

    function initDataTable(dataSource) {

        var dttable = $('#tables').dataTable(); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#tables").DataTable({
            // "order": false,
            "autoWidth": false,
            "ordering": false,
            "searching": false,
            "paging": false,
            "retrieve": true,
            "destroy": true,
            "bAutoWidth": true,
            "bLengthChange": true,
            "aLengthMenu": false,
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
            },
            data: dataSource,
            columnDefs: [
                {
                    "visible": true,
                    "targets": 0
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[0]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 0
                },

                {
                    "render": function (data, type, row, meta) {
                        var val = row[1]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 1
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[2]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 2
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[3]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 3
                },


                {
                    "render": function (data, type, row, meta) {
                        var val = '<a onclick="edits(\''+row[2]+'\',\''+row[3]+'\',\''+row[4]+'\')">编辑</a>'

                        return val;
                    },
                    "targets": 4
                },


            ]
        });
//权限控制函数
        power()
    };
}
function edits(businessUserName,businessUserPhone,orgId) {
    $("#businessStaffings").modal("show");
    $("#businessUserName").val(businessUserName)
    $("#businessUserPhone").val(businessUserPhone)
    $("#orgId").val(orgId)


}
//确定
function determine() {
    if($("#businessUserName").val()==""&&$("#businessUserPhone").val()==""){
        $("#businessUserNames").css({
            display: "inline"
        })
        $("#businessUserPhones").css({
            display: "inline"
        })
        return false;
    }else if($("#businessUserName").val()!=""&&$("#businessUserPhone").val()==""){
        $("#businessUserNames").css({
            display: "none"
        })
        $("#businessUserPhones").css({
            display: "inline"
        })
        return false;
    }else if($("#businessUserName").val()==""&&$("#businessUserPhone").val()!=""){
        $("#businessUserNames").css({
            display: "inline"
        })
        $("#businessUserPhones").css({
            display: "none"
        })
        return false;
    }else {
        $("#businessUserNames").css({
            display: "none"
        })
        $("#businessUserPhones").css({
            display: "none"
        })
    }


    $.ajax({
        url:root.member.updateOrg,
        cache: false,  //禁用缓存
        type: "POST",
        dataType:'json',
        contentType: "application/json",
        data: JSON.stringify({
            "orgId":$("#orgId").val(),
            "name": $("#businessUserName").val(),
            "phone":$("#businessUserPhone").val(),
        }),
        success: function (data) {
                if(data.errorCode==0) {
                    $.Huimodalalert('编辑成功',2000);
                    $("#businessStaffings").modal("hide");
                    searchsdfs()
                }
        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    });
}
//取消
function closeodel() {
    $("#businessStaffings").modal("hide");
    $("#businessUserNames").css({
        display: "none"
    })
    $("#businessUserPhones").css({
        display: "none"
    })
}
//限制字符
var len
var Preser
var common=10//要限制输入的字数
function checkMaxIut(me) {
    var text=$(me).val()
    strlen(text)
    if(len>common){
        $(me).val(Preser)
    }else {
        Preser=$(me).val()
    }
}
function strlen(str){
    len = 0;
    for (var i=0; i<str.length; i++) {
        var c = str.charCodeAt(i);
        //单字节加1
        if ((c >= 0x0001 && c <= 0x007e) || (0xff60<=c && c<=0xff9f)) {
            len++;
        } else {
            len+=2;
        }
    }
    return len;
}